package com.lekang.le.service.impl;

import java.util.Date;
import java.util.List;
import com.lekang.common.utils.DateUtils;
import com.lekang.le.domain.AccraditationRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.le.mapper.ContractMapper;
import com.lekang.le.domain.Contract;
import com.lekang.le.service.IContractService;

/**
 * 合同跟踪Service业务层处理
 * 
 * @author szh
 * @date 2025-10-13
 */


@Service
public class ContractServiceImpl implements IContractService 
{
    @Autowired
    private ContractMapper contractMapper;

    @Override
    public void calculateAndUpdateStatus(Long id) {
        Contract contract = contractMapper.selectContractById(id);
        if (contract == null) return;
        String status = calculateContractStatus(contract.getStartDate(), contract.getEndDate(), contract.getTerminationTime());
        contractMapper.updateContractStatus(id, status);
    }
    @Override
    public void batchCalculateAndUpdateStatus() {
        List<Contract> contracts = contractMapper.selectContractList(new Contract());
        for (Contract contract : contracts) {
            String status = calculateContractStatus(contract.getStartDate(), contract.getEndDate(), contract.getTerminationTime());
            contractMapper.updateContractStatus(contract.getId(), status);
        }
    }
    /**
     * 查询合同跟踪
     * 
     * @param id 合同跟踪主键
     * @return 合同跟踪
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectContractById(id);
    }

    /**
     * 查询合同跟踪列表
     * 
     * @param contract 合同跟踪
     * @return 合同跟踪
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同跟踪
     * 
     * @param contract 合同跟踪
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {

        contract.setCreateTime(DateUtils.getNowDate());
        contract.setContractStatus(calculateContractStatus(contract.getStartDate(), contract.getEndDate(), contract.getTerminationTime()));
        return contractMapper.insertContract(contract);
    }

    /**
     * 修改合同跟踪
     * 
     * @param contract 合同跟踪
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
     AccraditationRecordServiceImpl accraditationRecordServiceImpl = new AccraditationRecordServiceImpl();
//        System.out.println("contract:::::::::::::::::"+contract);
        contract.setContractStatus(calculateContractStatus(contract.getStartDate(), contract.getEndDate(), contract.getTerminationTime()));
        return contractMapper.updateContract(contract);
    }
    /** 合同状态计算核心逻辑（与前端完全对齐） */
    private String calculateContractStatus(Date startDate, Date endDate, Date terminationTime) {
        Date now = new Date();
        // 已失效（存在解除时间）
        if (terminationTime != null) {
            return "已失效";
        }
        // 未生效：当前时间 < 合同开始时间
        if (now.before(startDate)) {
            return "未生效";
        }
        // 生效中：合同开始时间 ≤ 当前时间 < 合同结束时间
        else if (now.after(startDate) && now.before(endDate)) {
            return "生效中";
        }
        // 已过期：当前时间 ≥ 合同结束时间
        else if (now.after(endDate)) {
            return "已过期";
        }
        return "未知";
    }

    /**
     * 批量删除合同跟踪
     * 
     * @param ids 需要删除的合同跟踪主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return contractMapper.deleteContractByIds(ids);
    }

    /**
     * 删除合同跟踪信息
     * 
     * @param id 合同跟踪主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return contractMapper.deleteContractById(id);
    }

    /**
     * 根据微信用户ID查询合同列表
     * 
     * @param wxUserId 微信用户ID
     * @return 合同列表
     */
    @Override
    public List<Contract> selectContractListByWxUserId(Long wxUserId)
    {
        return contractMapper.selectContractListByWxUserId(wxUserId);
    }
}
